package com.yl.core.p6spy;

import com.p6spy.engine.spy.appender.SingleLineFormat;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * p6spy打印日志输出格式修改
 * 1.只打印最终执行的sql.
 * 2.sql换到下一行
 * 3.结尾处增加分号,以标示sql结尾
 *
 * @Author jiangtao
 * @CreateBy 2019-07-25 2:10 PM
 * @UpdateBy jiangtao
 * @Version v1.0
 */
public class P6LogFormat extends SingleLineFormat {

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {

        if (!"".equals(sql.trim())) {
            return format.format(new Date()) + " | took " + elapsed + "ms | " + category + " | connection " + connectionId + "\n " + sql + "; \n --- END ---";
        } else {
            //old
            return "\n --- END ---";
            //return super.formatMessage(connectionId, now, elapsed, category, prepared, sql, url);
        }
    }
}
